package com.hustzp.com.xichuangzhu.login;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.SignUpCallback;
import com.hustzp.com.xichuangzhu.MainActivity;
import com.hustzp.com.xichuangzhu.R;
import com.hustzp.com.xichuangzhu.XCZBaseFragmentActivity;
import com.hustzp.com.xichuangzhu.utils.DialogFactory;

import org.json.JSONException;
import org.json.JSONObject;

public class LaststepActivity extends XCZBaseFragmentActivity {
    private EditText mPasswordView, userName;
    private TextView tMiddle, tLeft;
    private ImageView tLeftImage;
    public AVUser user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_laststep);
        initView();
        user = new AVUser();
    }

    public void initView() {
        userName = (EditText) findViewById(R.id.user_name);
        mPasswordView = (EditText) findViewById(R.id.password);
        tMiddle = (TextView) findViewById(R.id.middle_text);
        tLeftImage = (ImageView) findViewById(R.id.left_Image);
        tMiddle.setText(getResources().getString(R.string.the_last_step));
        tLeftImage.setOnClickListener(viewOnclick);
        findViewById(R.id.finish_button).setOnClickListener(viewOnclick);
    }

    public View.OnClickListener viewOnclick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int viewID = v.getId();
            switch (viewID) {
                case R.id.left_Image:
                    LaststepActivity.this.finish();
                    break;
                case R.id.finish_button:
                    registerUser();
                    break;
            }
        }
    };

    private void registerUser() {

        String userNameStr = userName.getText().toString().trim();
        String passWordStr = mPasswordView.getText().toString().trim();
        if (TextUtils.isEmpty(userNameStr) || TextUtils.isEmpty(passWordStr)) {
            DialogFactory.hintDialog(LaststepActivity.this, getResources().getString(R.string.input_info_no_completion));
            return;
        }
        user.setUsername(userNameStr);
        user.setPassword(passWordStr);
        // 其他属性可以像其他AVObject对象一样使用put方法添加
//        user.put("mobilePhoneNumber", user.getMobilePhoneNumber());
        user.signUpInBackground(new SignUpCallback() {
            public void done(AVException e) {
                if (e == null) {
                    Intent intent = new Intent(LaststepActivity.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.setAction("loginSuccess");
                    startActivity(intent);
                    finish();
                    // successfully
                } else {
                    String message = e.getMessage();
                    if(!TextUtils.isEmpty(message)){
                        try {
                            JSONObject resultObject = new JSONObject(message);
                            String code = resultObject.getString("code");
                            if("137".equals(code) || "214".equals(code)){

                                DialogFactory.hintDialog(LaststepActivity.this, LaststepActivity.this.getResources().getString(R.string.number_is_registed));
//                                DialogFactory.hintDialog(LaststepActivity.this, message);

                            }
                        } catch (JSONException e1) {
                            e1.printStackTrace();
                        }
                    }

                    // failed
                }
            }
        });
    }
}
